// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class Test_Project_Invoices_Change {
	static testMethod void TestProjectInvoiceCreated() {	 
		Id OppId;
		Id SecondInvoiceId;
		
		Opportunity firstOpp = new Opportunity (Name='test opp',StageName='Prospecting',CloseDate=System.today());
		insert firstOpp;
		OppId = firstOpp.id;
		
		Project_Invoice__c firstInvoice = new Project_Invoice__c (
			Name='Invoice 1',
			Opportunity__c=OppId,
			Amount__c=600
		);
		insert firstInvoice;
		Opportunity[] OppAfterInvoice = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(600,OppAfterInvoice[0].Invoiced_Amount__c);
		
		Project_Invoice__c secondInvoice = new Project_Invoice__c (
			Name='Invoice 2',
			Opportunity__c=OppId,
			Amount__c=800
		);
		insert secondInvoice;
		SecondInvoiceId = secondInvoice.id;
		
		Opportunity[] OppAfter2ndInvoice = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(1400,OppAfter2ndInvoice[0].Invoiced_Amount__c);
		//a household should be automatically created	
		
		secondInvoice.Amount__c=900;
		update secondInvoice;
		
		Opportunity[] OppAfter2ndInvoiceChange = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(1500,OppAfter2ndInvoiceChange[0].Invoiced_Amount__c);

		Project_Invoice__c thirdInvoice = new Project_Invoice__c (
			Name='Invoice 3',
			Opportunity__c=OppId,
			Amount__c=null
		);
		insert thirdInvoice;
		
		
		Opportunity[] OppAfterNullInvoice = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(1500,OppAfterNullInvoice[0].Invoiced_Amount__c);
		
		//delete firstTaskLog;
		delete secondInvoice;
		
		Opportunity[] OppAfterInvoiceDelete = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(600,OppAfterInvoiceDelete[0].Invoiced_Amount__c);
		
	//	Project_Invoice__c[] DeletedProject = [select id from Project_Invoice__c where Id=:SecondInvoiceId ALL ROWS];
		
	//	undelete DeletedProject[0];
		
	//	Opportunity[] OppAfterInvoiceUndelete = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
	//	System.assertEquals(1500,OppAfterInvoiceUndelete[0].Invoiced_Amount__c);
		
		
		//delete firstTaskLog;
		delete firstInvoice;
		
		Opportunity[] OppAfter1stInvoiceDelete = [select Invoiced_Amount__c from Opportunity where Id =:OppId];
		System.assertEquals(0,OppAfter1stInvoiceDelete[0].Invoiced_Amount__c);
		
		
	}
}